<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Register extends CI_Controller {

    private $data;
	private $countryList; 
	
    public function __construct()
	{
	    parent::__construct();
		$this->load->model('system/country_m','country_m',TRUE);
		$this->load->model('system/user_m','user_m',TRUE);
		$this->load->helper('form');
		$this->load->helper('url');		
		
		// Load countries
		$countries = $this->country_m->get_list();
		$options = array( '' => 'Choose country' );
		foreach($countries as $country){
		   $options[$country['id']] = $country['name'];
		}
		$this->countryList = $options;
	}
	
    public function index(){
	    if(!empty($_POST)){
             return $this->submit();
		}
		
		$this->data['baseUrl'] =  $this->config->item('base_url');
		$this->data['loggedusername'] = $this->session->userdata(LOGGEDUSERNAME);
		$this->data['header'] = 'system/_template/header_anon_v';
		$this->data['content'] = 'system/register_v';
		$this->data['title'] = 'Register';
		
		
		$this->data['countries'] = $this->countryList;
		
		try{
			$this->load->view('system/_template/template_v', $this->data);
		}
		catch(Exception $e)
		{
			$this->load->helper('url');
			show_404();
		}
	}
	public function choose_select ( $str ){
	    if ( trim($str) == '' ) {
			$this->form_validation->set_message('choose_select', '%s is required');
			return FALSE;

		}
		return TRUE;
	}
	
	private function _strong_password ( $str ) {
	    // Must have uppercase, lowercase, and number,
		// Must minimum 8 length characters
	    if(!$this->_valid_pass($str)){
		    $this->form_validation->set_message('strong_password', 'Password not strong enough');
			return FALSE;
		}
		return TRUE; 
	}

	private function _valid_pass($candidate) {
		   $r1='/[A-Z]/';  //Uppercase
		   $r2='/[a-z]/';  //lowercase
		   $r4='/[0-9]/';  //numbers
		
		   if(preg_match_all($r1,$candidate, $o)<1) return FALSE;
		
		   if(preg_match_all($r2,$candidate, $o)<1) return FALSE;
		
		   if(preg_match_all($r4,$candidate, $o)<1) return FALSE;
		
		   if(strlen($candidate)<8) return FALSE;
		
		   return TRUE;
	}
	
	public function submit(){
		if(!empty($_POST)){
			 $this->load->library('form_validation');
			 $this->form_validation->set_message('is_unique','%s is already used');
			 $this->form_validation->set_error_delimiters('<li>', '</li>');
			 // Validation rule check on 'application/config/form_validation.php'
			 
			 try {
			 	if($this->form_validation->run() == TRUE){
				    $username = $this->input->post('username');
					$email = $this->input->post('email');
					$password = $this->config->item('salt') .$this->input->post('password');
					$first_name = $this->input->post('first_name');
					$last_name = $this->input->post('last_name');
					$gender = $this->input->post('gender');
					$phone = $this->input->post('phone');
					$address1 = $this->input->post('address1');
					$address2 = $this->input->post('address2');
					$city = $this->input->post('city');
					$province = $this->input->post('province_state');
					$country = $this->input->post('country');
					$postal_code = $this->input->post('postal_code');
					$facebook = $this->input->post('facebook');
					$twitter = $this->input->post('twitter');
					$website = $this->input->post('website');
					
				    $this->user_m->register( $username, $email, $password, $first_name, $last_name, $gender, $phone, $address1, $address2, $city, $province, $country, $postal_code, $facebook, $twitter, $website);
				
				    $this->data['content'] = 'messages_v';
				    $this->data['msgSuccess'] = "Register success";
				}else{
		            $this->data['content'] = 'system/register_v';
					$this->data['msgError'] = validation_errors();
				}
			 }catch(Exception $e){
				$error = $e->getMessage();
	            $this->data['content'] = 'system/register_v';
				$this->data['msgError'] = "Save Failed - ".$error;
			 }
			 
			 $this->data['baseUrl'] =  $this->config->item('base_url');
 		     $this->data['header'] = 'system/_template/header_anon_v';
		     $this->data['title'] = 'Register';
	 		 $this->data['countries'] = $this->countryList;
		     
			 //$this->data['nextUrl'] = 'Register';
			try{
				$this->load->view('system/_template/template_v', $this->data);
			}
			catch(Exception $e)
			{
				$this->load->helper('url');
				show_404();
			}	
		}
	}
	
}

?>